/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pt.our.angemc.datatypes;

import java.util.ArrayList;

/**
 *
 * @author piskeiro
 */
public class Rule {
    private final String ruleTerminator = ";";
    private final String rule2productionsSeparator = ":";
    private final String productionsSeparator = "|";

    //symbol
    private Symbol leftSymbol;
    private ArrayList<Production> productions;

    //atributos

    private ArrayList<Variable> atribsIN;
    private ArrayList<Variable> atribsOUT;

    public Rule(Symbol leftSymbol, ArrayList<Production> productions, ArrayList<Variable> atribsIN, ArrayList<Variable> atribsOUT) {
        this.leftSymbol = leftSymbol;
        this.productions = productions;
        this.atribsIN = atribsIN;
        this.atribsOUT = atribsOUT;
    }

    

    public Symbol getLeftSymbol() {
        return leftSymbol;
    }

    public ArrayList<Production> getProductions() {
        return productions;
    }

    public void setLeftSymbol(Symbol leftSymbol) {
        this.leftSymbol = leftSymbol;
    }

    public void setProductions(ArrayList<Production> productions) {
        this.productions = productions;
    }

    public void addProduction(Production production){
        productions.add(production);
    }

    public ArrayList<Variable> getAtribsIN() {
        return atribsIN;
    }

    public ArrayList<Variable> getAtribsOUT() {
        return atribsOUT;
    }

    @Override
    public Rule clone(){
        ArrayList<Production> newProductions = new ArrayList<Production>();

        ArrayList<Variable> newAtribIN = new ArrayList<Variable>();
        ArrayList<Variable> newAtribOUT = new ArrayList<Variable>();

        for(Production production : productions){
            newProductions.add(production.clone());
        }

        if(this.getAtribsIN() != null) {
        for(Variable v : this.getAtribsIN()){ newAtribIN.add(v.clone());} }
        
        if(this.getAtribsOUT() != null) {
        for(Variable v : this.getAtribsOUT()){ newAtribOUT.add(v.clone());}}

        Rule rule = new Rule(leftSymbol.clone(), newProductions, newAtribIN, newAtribOUT);
        return rule;
    }

    @Override
    public String toString(){
        int i=0;
        StringBuffer sb = new StringBuffer();
        sb.append(leftSymbol.toString());

        if(this.getAtribsIN() != null && !this.getAtribsIN().isEmpty())
        {
            int j = 0;
            sb.append("[");
            for(Variable v : this.getAtribsIN())
            {
                if(j==1) { sb.append(", "); }
                sb.append(v.toString());
                j = 1;
            }
            sb.append("]");
        }

        if(this.getAtribsOUT() != null && !this.getAtribsOUT().isEmpty())
        {
            int j = 0;
            sb.append(" returns ");
            sb.append("[");
            for(Variable v : this.getAtribsOUT())
            {
                if(j==1) { sb.append(", "); }
                sb.append(v.toString());
                j = 1;
            }
            sb.append("]");
        }

        for(Production production : productions){
            sb.append("\t"+(i==0?rule2productionsSeparator:productionsSeparator)+" "+production.toString()+"\n");
            i++;
        }
        sb.append("\t"+this.ruleTerminator+"\n");
        return sb.toString();
    }


    
}
